<template>
  <div class="login-container">
    <div class="login-wrapper">
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        label-position="right"
      >
        <h2 class="form-title">用户登录页面</h2>

        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="账号"
            :prefix-icon="User"
            size="large"
            class="custom-input"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            size="large"
            class="custom-input"
            show-password
          />
        </el-form-item>

        <el-form-item class="remember-item">
          <el-checkbox v-model="loginForm.rememberMe">
            <span class="remember-text">记住密码</span>
          </el-checkbox>
        </el-form-item>

        <el-form-item class="action-btns">
          <el-button
            type="primary"
            size="large"
            @click="handleLogin"
            class="login-btn"
          >
            登录
          </el-button>
          <el-button
            type="warning"
            size="large"
            @click="handleRegister"
            class="register-btn"
          >
            暂无账号？去注册
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { User, Lock } from "@element-plus/icons-vue";
import type { LoginForm } from "@/types/login";
import { login } from "@/api/login";
import Cookies from "js-cookie";

import bcrypt from "bcryptjs";
const route = useRoute();
const router = useRouter();
const loginForm = reactive<LoginForm>({
  username: "",
  password: "",
  rememberMe: false,
});

const loginRules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 5, message: "长度在3到5个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 16, message: "长度在6到16个字符", trigger: "blur" },
  ],
});

const handleLogin = () => {
  if (loginForm.rememberMe) {
    Cookies.set("username", loginForm.username, { expires: 30 });
    Cookies.set("password", bcrypt.hashSync(loginForm.password, 10), {
      expires: 30,
    });
    Cookies.set("rememberMe", String(loginForm.rememberMe), { expires: 30 });
  } else {
    // 否则移除
    Cookies.remove("username");
    Cookies.remove("password");
    Cookies.remove("rememberMe");
  }
  console.log(loginForm);
  login(loginForm).then((res) => {
    console.log(res);
  });
};

const handleRegister = () => {
  router.push("/reg");
};
</script>

<style lang="scss" scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;

  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);

  &::before {
    content: "";
    position: absolute;
    top: -10%;
    right: -10%;
    width: 40%;
    height: 120%;
    background: rgba(255, 255, 255, 0.2);
    transform: rotate(-15deg);
    z-index: 0;
  }

  .login-wrapper {
    position: relative;
    width: 1200px;
    height: 100%;
    margin: 0 auto;
    display: flex;
    align-items: center;
    z-index: 1;

    .login-form {
      position: absolute;
      right: 10%;
      bottom: 25%;
      width: 380px;
      padding: 40px;
      background: rgba(255, 255, 255, 0.95);
      border-radius: 12px;
      box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);

      .form-title {
        margin-bottom: 30px;
        font-size: 24px;
        color: #2c3e50;
        text-align: center;
        letter-spacing: 1px;
      }

      :deep(.custom-input) {
        .el-input__wrapper {
          background: #f8f9fa;
          border-radius: 8px;
          box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);

          &.is-focus {
            box-shadow: 0 0 0 1px #409eff inset;
          }
        }
      }

      .remember-item {
        margin-bottom: 25px;

        :deep(.el-checkbox__label) {
          color: #666;
        }
      }

      .action-btns {
        .el-button {
          width: 100%;
          margin: 10px 0;
          height: 20px;
          border-radius: 8px;
          font-weight: 500;
          letter-spacing: 1px;

          &.login-btn {
            background: linear-gradient(135deg, #409eff, #337ecc);
            border: none;
          }

          &.register-btn {
            background: linear-gradient(135deg, #e6a23c, #b88230);
            border: none;
          }
        }
      }
    }
  }
}

@media (max-width: 1200px) {
  .login-wrapper {
    width: 100% !important;
    justify-content: center;

    .device-group {
      display: none;
    }

    .login-form {
      position: static !important;
      width: 90% !important;
      max-width: 500px;
      margin: 0 auto;
    }
  }
}
</style>
